<?php

namespace app\home\controller\user;

//引入公共控制器
use app\common\controller\Home;

//引入Cookie
use think\Cookie;

class User extends Home
{

  public function __construct()
  {
      //继承父类
      parent::__construct();

      //加载模型
      $this->BusinessModel = model('Business.Business');
      $this->RegionModel = model('Region');

  }


  public function index()
  {
    // 查询数据
    $business = $this->BusinessModel->find($this->auth->id);

    

    $this->assign([
      'business' => $business,
      'avatar'=>is_file(ltrim($business['avatar'],'/')) ? $business['avatar'] : '/assets/home/images/avatar.jpg'
    ]);
   

    return $this->view->fetch();
  }


  public function profile()
  {
    
    $id = isset($this->auth['id']) ? $this->auth['id'] : 0;
    // 省份
    $province = $this->RegionModel->where('code', $this->auth['province'])->value('name');

    // getLastSql()调式sql语句 打印原生sql语句
    // echo  $this->RegionModel->getLastSql();exit;
    // 市
    $city = $this->RegionModel->where('code', $this->auth['city'])->value('name');
    // 区
    $district = $this->RegionModel->where('code', $this->auth['district'])->value('name');

      // 查询数据
      $business = $this->BusinessModel->find($this->auth->id);

      if($this->request->isPost())
      {
        $params = $this->request->param();
        $password = $this->request->param('password', '');
        $data=[
          'id' => $business['id'],
          'mobile' => $params['mobile'],
          'gender' => $params['gender'],
          'nickname' => $params['nickname'],
         
        ];

         // 修改省市区
         if($params['region']){
          $region = explode('/',$params['region']);
          if(count($region) < 3){
              $this->error('请重新选择省市区!');
          }
          $pathcode = $this->RegionModel->where('name',$region[2])->value('parentpath');
          $pathcode = explode(',',$pathcode);
          list($province, $city, $district) = $pathcode;
          $data['province'] = $province;
          $data['city'] = $city;
          $data['district'] = $district;
         
      }

         // 修改密码
         if($password){
          $data['password'] = md5($params['password'].$business['salt']);
          }

     
        $imgUrl = upload('avatar');

       if($imgUrl != false){
        
        $data['avatar'] = "/uploads/".$imgUrl;
       }
      
      

        $result = $this->BusinessModel->isUpdate()->save($data);

        if($result === FALSE){
          if(isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0){
          //上传上来的图片删除了
          @is_file('.'.$data['avatar']) && @unlink('.'.$data['avatar']);
          }
          $this->error($this->BusinessModel->getError());
          exit;

        }else{
          if(isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0){
          //修改成功后删除
          $avatar = ltrim($business['avatar'],"/");

          // echo $avatar;
          // exit;
            @is_file($avatar) && @unlink($avatar);

          }

          $this->success(__('更新成功'),url('/home/user/user/index'));

        }

           
    
      }

   
        // var_dump($business);
        // exit;

      $this->assign([
        'business' => $business,
        'province' => $province,
        'city' => $city,
        'district' => $district,
        
      ]);
     
     
      return $this->view->fetch();
  }  
      // 查询市 区 
      public function area(){
        if($this->request->isPost()){
            $code = $this->request->param('code');

            $result = $this->RegionModel->where(['parentid' => $code])->select();

           //返回json数据
           return json($result);
        }
    }
    public function contact()
    {
      return $this->view->fetch();
    }
  
}


